home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / sb / sndcon.zip / SNDCONV.C next >
C/C++ Source or Header  |  1992-05-04  |  4KB  |  198 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys\stat.h>
  5. #include <io.h>
  6.  
  7. void        main(int argc,char **argv);
  8. void        getSoundInfo(char *name,int type);
  9. void        WriteWaveHeader(FILE *fh);
  10.  
  11. enum filetypes { RAW, GSS, VOC, WAV, SND, VMD };
  12.  
  13. #define NUMTYPES 6
  14. char        *typelist[] = {
  15.                                                 "RAW",
  16.                                                 "GSS",
  17.                                                 "VOC",
  18.                                                 "WAV",
  19.                                                 "SND",
  20.                                                 "VMD"
  21.  
  22.                                             };
  23. int            skipsize[] =  {
  24.                                                 0x00,
  25.                                                 0x40,
  26.                                                 0x20,
  27.                                                 0x2C,
  28.                                                 0x00,
  29.                                                 0x00
  30.                                             };
  31.  
  32. typedef struct
  33. {
  34.     long        length;
  35.     long        samprate;
  36. } SOUND;
  37.  
  38. SOUND        sinfo;
  39. long        gSampleRate;
  40.  
  41.  
  42. void        main(int argc,char **argv)
  43. {
  44.     FILE        *infh,*outfh;
  45.     int            xcon,i,skip,intype,outtype;
  46.     unsigned read;
  47.     char        inname[13],outname[13],t[13],t1[13],inext[5],outext[5];
  48.     char        bffr[50000U];
  49.  
  50.     if (argc < 3 || argc > 4)     // Means 3 or 4 parameters accepted
  51.     {
  52.         printf("Usage: %s (fromname.ext) (toname.ext) [samplerate]\n",argv[0]);
  53.         printf("       samplerate is 11000 if not entered.\n");
  54.         printf("Valid input file types: .WAV a Microsoft WAVE file.\n");
  55.         printf("                       .VOC a Soundblaster RAW .VOC file.\n");
  56.         printf("                       .GSS ??\n");
  57.         printf("                       .SND A raw file.\n");
  58.         printf("                       .RAW A raw file.\n");
  59.         printf("                       .VMD A raw file.\n");
  60.         printf("Valid output types are .WAV and .RAW.  Other file\n");
  61.         printf("formats to be added at a later date.\n");
  62.         exit(1);
  63.     }
  64.  
  65.     strcpy(t,argv[1]);
  66.     strcpy(inname,argv[1]);
  67.     strcpy(t1,strtok(t,"."));
  68.     strcpy(inext,strtok(NULL,"."));
  69.     strupr(inext);
  70.     for (i=0,intype=0; i<NUMTYPES; i++)
  71.     {
  72.         if (!strcmp(typelist[i],inext))
  73.         {
  74.             intype = i;
  75.             break;
  76.         }
  77.     }
  78.  
  79.     strcpy(t,argv[2]);
  80.     strcpy(outname,argv[2]);
  81.     strcpy(t1,strtok(t,"."));
  82.     strcpy(outext,strtok(NULL,"."));
  83.     strupr(outext);
  84.     for (i=0,outtype=0; i<NUMTYPES; i++)
  85.     {
  86.         if (!strcmp(typelist[i],outext))
  87.         {
  88.             outtype = i;
  89.             break;
  90.         }
  91.     }
  92.  
  93.     if (access(inname,0) == -1)
  94.     {
  95.         printf("Error: File not found: %s\n",inname);
  96.         exit(1);
  97.     }
  98.  
  99.     gSampleRate = 11000L;
  100.     if (argc == 4)
  101.     {
  102.         gSampleRate = atol(argv[2]);
  103.     }
  104.  
  105.     getSoundInfo(inname,intype);
  106.  
  107.  
  108.     infh = fopen(inname,"rb");
  109.     outfh = fopen(outname,"wb");
  110.  
  111.     skip = skipsize[intype];
  112.     fread(bffr,1,skip,infh);            // Skip header stuff from input format
  113.  
  114.     switch (outtype)
  115.     {
  116.         case RAW:
  117.             break;
  118.         case GSS:
  119.             break;
  120.         case VOC:
  121.             break;
  122.         case WAV:
  123.             WriteWaveHeader(outfh);
  124.     }
  125.  
  126.     xcon = 0;
  127.     do
  128.     {
  129.         read = fread(bffr,1,50000U,infh);
  130.         if (read)
  131.             fwrite(bffr,1,read,outfh);
  132.         else
  133.             xcon = 1;
  134.     } while (!xcon);
  135. }
  136.  
  137.  
  138. void        getSoundInfo(char *name,int type)
  139. {
  140.     FILE        *fh;
  141.     struct stat statbuf;
  142.  
  143.     stat(name,&statbuf);
  144.  
  145.     switch (type)
  146.     {
  147.         case RAW:
  148.         case SND:
  149.         case VMD:
  150.             sinfo.length = statbuf.st_size;
  151.             sinfo.samprate = gSampleRate;
  152.             break;
  153.         case GSS:
  154.             sinfo.length = statbuf.st_size - (long)skipsize[type];
  155.             sinfo.samprate = gSampleRate;
  156.             break;
  157.         case VOC:
  158.             sinfo.length = statbuf.st_size - (long)(skipsize[type]+1);
  159.             sinfo.samprate = gSampleRate;
  160.             break;
  161.         case WAV:
  162.             fh = fopen(name,"rb");
  163.             fseek(fh,24L,SEEK_SET);                    // Offset to Sample rate
  164.             fread(&sinfo.samprate,4,1,fh);    // Read Sample rate
  165.             fseek(fh,40L,SEEK_SET);                    // Offset to Sample length
  166.             fread(&sinfo.length,4,1,fh);        // Read Sample length
  167.             fclose(fh);
  168.             break;
  169.     }
  170. }
  171.  
  172.  
  173. void        WriteWaveHeader(FILE *fh)
  174. {
  175.     long        bl;
  176.     int            bi;
  177.  
  178.     fwrite("RIFF",4,1,fh);            // Write "RIFF"
  179.     bl = sinfo.length + 36L;
  180.     fwrite(&bl,4,1,fh);                    // Write Size of file with header
  181.     fwrite("WAVE",4,1,fh);            // Write "WAVE"
  182.     fwrite("fmt ",4,1,fh);            // Write "fmt "
  183.     bl = 16L;
  184.     fwrite(&bl,4,1,fh);                    // Size of previous header (fixed)
  185.     bi = 1;
  186.     fwrite(&bi,2,1,fh);                    // formatTag
  187.     fwrite(&bi,2,1,fh);                    // nChannels
  188.     bl = sinfo.samprate;
  189.     fwrite(&bl,4,1,fh);                    // nSamplesPerSec
  190.     fwrite(&bl,4,1,fh);                 // nAvgBytesPerSec
  191.     fwrite(&bi,2,1,fh);                    // nBlockAlign (always 1?)
  192.     bi = 8;
  193.     fwrite(&bi,2,1,fh);                    // nBitsPerSample (8 or 16 I assume)
  194.     fwrite("data",4,1,fh);            // Write "data"
  195.     bl = sinfo.length;
  196.     fwrite(&bl,4,1,fh);                    // True length of sample data
  197. }
  198.